package com.sgb.knowledgeshare.view.activity;

import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import com.aliyun.player.AliPlayer;
import com.aliyun.player.AliPlayerFactory;
import com.aliyun.player.IPlayer;
import com.aliyun.player.source.UrlSource;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareVideoBinding;
import com.sgb.knowledgeshare.entity.VideoFileBean;
import com.sgb.knowledgeshare.utils.RxBus;
import com.swgk.core.base.AppActivity;

/**
 * Created by：冯齐 on 2020/11/23 11:06
 * Description：
 */
public class ShareVideoInfoActivity extends AppActivity {
    private ActivityShareVideoBinding binding;
    private VideoFileBean bean;
    private AliPlayer aliyunVodPlayer;
    private boolean islooks = true;

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_video);
        bean = (VideoFileBean) getIntent().getSerializableExtra("bean");
        if (bean != null) {
            //根据状态动态设置是否选中状态
            if (bean.isSelect()) {
                binding.editImagev.setImageResource(R.mipmap.share_user_video_select);
            } else {
                binding.editImagev.setImageResource(R.mipmap.share_user_video_no);
            }
        }
        initVideoView();
        onclickView();
    }

    //view处理
    private void initVideoView() {
        aliyunVodPlayer = AliPlayerFactory.createAliPlayer(getApplicationContext());
        aliyunVodPlayer.setAutoPlay(true);
        aliyunVodPlayer.setLoop(true);
        UrlSource urlSource = new UrlSource();
        urlSource.setUri(bean.getPath());
        aliyunVodPlayer.setDataSource(urlSource);
        //准备播放
        aliyunVodPlayer.prepare();
        binding.videoTextureview.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                Surface mSurface = new Surface(surface);
                if (aliyunVodPlayer != null) {
                    aliyunVodPlayer.setSurface(mSurface);
                    aliyunVodPlayer.redraw();
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                if (aliyunVodPlayer != null) {
                    aliyunVodPlayer.redraw();
                }
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                if(aliyunVodPlayer != null){
                    aliyunVodPlayer.setSurface(null);
                }
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });
        aliyunVodPlayer.setOnCompletionListener(new IPlayer.OnCompletionListener() {
            @Override
            public void onCompletion() {
                //播放完成事件
                // 开始播放。
                islooks= true;
                aliyunVodPlayer.start();
                binding.ivPlays.setVisibility(View.GONE);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
      aliyunVodPlayer.release();
    }

    private void onclickView() {
        //返回
        binding.backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aliyunVodPlayer.stop();
                binding.ivPlays.setVisibility(View.VISIBLE);
                if (bean != null) {
                    RxBus.getDefault().post(bean);
                }
                finish();
            }
        });
        //选择
        binding.videoSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setSelect(!bean.isSelect());
                if (bean.isSelect()) {
                    binding.editImagev.setImageResource(R.mipmap.share_user_video_select);
                } else {
                    binding.editImagev.setImageResource(R.mipmap.share_user_video_no);
                }
            }
        });
        binding.videoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if (islooks){
                  //暂停播放
                  aliyunVodPlayer.pause();
                  binding.ivPlays.setVisibility(View.VISIBLE);
                  islooks =false;
              }else {
                  // 开始播放。
                  binding.ivPlays.setVisibility(View.GONE);
                  aliyunVodPlayer.start();
                  islooks=true;
              }
            }
        });
    }
}
